package com.itheima.health.service.impl;

import com.itheima.health.mapper.ReportMapper;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.ReportService;
import com.itheima.health.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Reference
    private MemberService memberService;

    @Reference
    private OrderService orderService;

    @Reference
    private SetmealService setmealService;

    //以日期为横坐标,会员人数为纵坐标,展示一个折线图在页面上
    @Override
    public Map memberReportByDate(String[] dateValue) {
        System.out.println("dateValue = " + dateValue);
        String startDate = dateValue[0]+"-1";
        String endDate = dateValue[1]+"-31";

        List<Map> listMap = reportMapper.memberReportByDate(startDate,endDate);

        Map hashMap = new HashMap<>();

        //创建一个方法对集合进行转换
        chengeMothed(listMap, hashMap);
        return hashMap;
    }

    @Override
    public List<Map> setmealProportion() {
        return reportMapper.setmealProportion();
    }

    @Override
    public List<Map> findMemberSex() {
        return reportMapper.findMemberSex();
    }

    @Override
    public List<Map> findMemberAge() {
        return reportMapper.findMemberAge();
    }


    //导出excel todoby dzf
    @Override
    public Boolean exportExcel(Map map) throws IOException, Exception {


        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet sheet = xssfWorkbook.createSheet("黑马");


        XSSFCellStyle Style = xssfWorkbook.createCellStyle();
        Style.setBorderTop(BorderStyle.THIN);
        Style.setBorderBottom(BorderStyle.THIN);
        Style.setBorderRight(BorderStyle.THIN);
        Style.setBorderLeft(BorderStyle.THIN);

        XSSFFont f = xssfWorkbook.createFont();
        f.setBold(true);


        XSSFCellStyle titleStyle = xssfWorkbook.createCellStyle();
        titleStyle.setFont(f);
        titleStyle.setAlignment(HorizontalAlignment.CENTER);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 7));

        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellStyle(titleStyle);
        cell.setCellValue("会员数据统计");


        XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();

        List<Integer> list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(8);


        XSSFRow row1 = sheet.createRow(1);

        row1.createCell(0).setCellValue("今日新增会员数");
        row1.createCell(2).setCellValue(map.get("todayNewMember") + "");
        row1.createCell(4).setCellValue("总会员数");
        row1.createCell(6).setCellValue(map.get("totalMember") + "");

        XSSFRow row2 = sheet.createRow(2);

        row2.createCell(0).setCellValue("本周新增会员数");
        row2.createCell(2).setCellValue(map.get("thisWeekNewMember") + "");
        row2.createCell(4).setCellValue("本月新增会员数");
        row2.createCell(6).setCellValue(map.get("thisMonthNewMember") + "");


        XSSFRow row3 = sheet.createRow(3);
        XSSFCell cell3 = row3.createCell(0);
        cell3.setCellStyle(titleStyle);
        cell3.setCellValue("预约到诊数据统计");


        XSSFRow row4 = sheet.createRow(4);
        row4.createCell(0).setCellValue("今日预约数");
        row4.createCell(2).setCellValue(map.get("todayOrderNumber") + "");
        row4.createCell(4).setCellValue("今日到珍数");
        row4.createCell(6).setCellValue(map.get("todayVisitsNumber") + "");

        XSSFRow row5 = sheet.createRow(5);
        row5.createCell(0).setCellValue("本周预约数");
        row5.createCell(2).setCellValue(map.get("thisWeekOrderNumber") + "");
        row5.createCell(4).setCellValue("本周到珍数");
        row5.createCell(6).setCellValue(map.get("thisWeekVisitsNumber") + "");

        XSSFRow row6 = sheet.createRow(6);
        row6.createCell(0).setCellValue("本月预约数");
        row6.createCell(2).setCellValue(map.get("thisMonthOrderNumber") + "");
        row6.createCell(4).setCellValue("本月到珍数");
        row6.createCell(6).setCellValue(map.get("thisMonthVisitsNumber") + "");


        XSSFRow row7 = sheet.createRow(7);
        XSSFCell cell7 = row7.createCell(0);
        cell7.setCellStyle(titleStyle);
        cell7.setCellValue("热门套餐");

        XSSFRow row8 = sheet.createRow(8);
        row8.createCell(0).setCellValue("套餐名称");
        row8.createCell(2).setCellValue("预约数量");
        row8.createCell(4).setCellValue("占比");
        row8.createCell(6).setCellValue("备注");

        List<Map> hotSetmeal = (List) map.get("hotSetmeal");
        int k = 0;
        for (Map setmealMap : hotSetmeal) {

            XSSFRow xssfRow = sheet.createRow(9 + k);
            list.add(9 + k++);
            xssfRow.createCell(0).setCellValue(setmealMap.get("name") + "");
            xssfRow.createCell(2).setCellValue(setmealMap.get("setmeal_count") + "");
            xssfRow.createCell(4).setCellValue(setmealMap.get("proportion") + "");
            xssfRow.createCell(6).setCellValue(setmealMap.get("remark") + "");
        }


        for (Integer rowNum : list) {

            for (int i = 0; i <= 6; i = i + 2) {
                sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, i, i + 1));
                if (i / 2 % 2 != 0) {
                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
                    sheet.getRow(rowNum).getCell(i).setCellStyle(cellStyle);
                } else {
                    cellStyle.setAlignment(HorizontalAlignment.RIGHT);
                    sheet.getRow(rowNum).getCell(i).setCellStyle(cellStyle);
                }

            }
        }

        //  写出去
        try {
            xssfWorkbook.write(new FileOutputStream(new File("d:\\heima.xlsx")));

            xssfWorkbook.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

        return true;
    }

    @Override
    public Map getBusinessReport() {

        HashMap<Object, Object> map = new HashMap<>();
        map.put("reportDate",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        map.put("totalMember",memberService.totalMember());
        map.put("todayNewMember",memberService.getDayNumber());
        map.put("thisWeekNewMember",memberService.getWeekNumber());
        map.put("thisMonthNewMember",memberService.getMonthNumber());
        map.put("todayOrderNumber",orderService.getDayReservations());
        map.put("todayVisitsNumber",orderService.getFinishDay());
        map.put("thisWeekOrderNumber",orderService.getWeekReservations());
        map.put("thisWeekVisitsNumber",orderService.getFinishWeek());
        map.put("thisMonthOrderNumber",orderService.getMonthReservations());
        map.put("thisMonthVisitsNumber",orderService.getFinishMonth());
        map.put("hotSetmeal",setmealService.getHostSetmeal());
        return map;
    }


    //遍历结合将数据封装到hashMap中
    private void chengeMothed(List<Map> listMap, Map hashMap) {
        //遍历将数据封装到hashMap
        ArrayList<Object> yearAndMonths = new ArrayList<>();
        ArrayList<Object> memberCounts = new ArrayList<>();
        for (Map map : listMap) {
            //封装
            yearAndMonths.add(map.get("yearAndMonths"));
            memberCounts.add(map.get("memberCounts"));
        }
        hashMap.put("yearAndMonths", yearAndMonths);
        hashMap.put("memberCounts", memberCounts);
    }
}
